namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Globalization;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Routing;
    using Xunit;

    public sealed class TenantControllerOfTFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TenantControllerOfTestResource>()
                .DerivesFrom<TenantController<TestResource>>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Implements<IControlResource<TestResource>>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TenantControllerOfTestResource());
        }

        [Fact]
        public void op_Register_RouteCollectionNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new TenantControllerOfTestResource() as IRegisterRoutes).Register(null));
        }

        [Fact]
        public void op_Register_RouteCollection()
        {
            RouteCollection routes = new RouteCollection();
            (new TenantControllerOfTestResource() as IRegisterRoutes).Register(routes);

            Assert.IsType<ResourceRoute<TestResource>>(routes[0]);
        }

        [Fact]
        public void op_Model_string_CultureInfo()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TestResource>(new
                {
                    urn = "urn://example.com/resource/test",
                    xml = "<resource title='Test' />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                TestResource actual = new TenantControllerOfTestResource().Model(new CultureInfo("en"));

                Assert.Equal<string>("Test", actual.Title);

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_Redirect_T()
        {
            ActionResult obj = new TenantControllerOfSeeOther().Redirect(new SeeOther("/path"));

            Assert.IsType<SeeOtherResult>(obj);
        }

        [Fact]
        public void op_HtmlRepresentation_CultureInfo()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TestResource>(new
                {
                    urn = "urn://example.com/resource/test",
                    xml = "<resource />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.IsType<ViewResult>(new TenantControllerOfTestResource().HtmlRepresentation(new CultureInfo("en")));

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_XhtmlRepresentation_CultureInfo()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TestResource>(new
                {
                    urn = "urn://example.com/resource/test",
                    xml = "<resource />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.IsType<ViewResult>(new TenantControllerOfTestResource().XhtmlRepresentation(new CultureInfo("en")));

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}